package cn.lyh.iotmanage.service;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.lyh.iotmanage.dao.EmqClientsDao;
import cn.lyh.iotmanage.dao.EmqClientsDaoMapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author lyh
 * @version 1.0
 * @since 2021/5/15
 */
@Service
public class EmqClientsDaoService extends ServiceImpl<EmqClientsDaoMapper, EmqClientsDao> {

    @Autowired
    EmqClientsDaoMapper emqClientsDaoMapper;

    /**
     * 此方法用于解析设备上线转发到服务器后台的数据
     * 1.先解析时间戳，转化为日期格式
     * 2.获取clientid
     * 3.判断clientid是否存在，若存在则执行更新操作，不存在则保存
     *
     * @param params 前端传入接收到的数据
     */
    @Cacheable(value = "clientInfo",key = "#params.get('clientid')")
    public void saveClientInfo(Map<String, Object> params) {
        // 1.解析时间戳参数，获取connected_at时间
        Object connectedAt = params.get("connected_at");
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String format = simpleDateFormat.format(connectedAt);
        DateTime parse = DateUtil.parse(format);
        System.out.println("parse = " + parse);
        // 2.获取clientId
        String clientId = (String) params.get("clientid");
        // 3.获取当前时间
        Date nowDate = DateUtil.date();
        System.out.println("nowDate = " + nowDate);
        // 4.封装成bean存入mysql
        EmqClientsDao emqClientsDao = new EmqClientsDao();
        emqClientsDao.setClientid(clientId);
        emqClientsDao.setOnlineAt(parse);
        emqClientsDao.setUpdateAt(nowDate);
        emqClientsDao.setIsOnline(true);

        // 5.先根据clientid进行查询，看是否已经存在该设备信息，假如存在则根据clientid去更新数据，若不存在则保存新的信息
        this.saveOrUpdate(emqClientsDao, new QueryWrapper<EmqClientsDao>().eq(EmqClientsDao.COL_CLIENTID, clientId));
    }

    public void saveDisconnectedInfo(Map<String, Object> params) {
        // 1.解析时间戳参数，获取disconnectedAt时间
        Object disconnectedAt = params.get("disconnected_at");
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String format = simpleDateFormat.format(disconnectedAt);
        DateTime parse = DateUtil.parse(format);
        // 2.获取clientId
        String clientId = (String) params.get("clientid");
        // 3.获取当前时间
        Date nowDate = DateUtil.date();
        System.out.println("nowDate = " + nowDate);
        // 4.封装成bean存入mysql
        EmqClientsDao emqClientsDao = new EmqClientsDao();
        emqClientsDao.setUpdateAt(nowDate);
        emqClientsDao.setIsOnline(false);
        emqClientsDao.setOfflineAt(parse);
        // 5下线为更新操作，根据clientid去更新
        this.saveOrUpdate(emqClientsDao, new QueryWrapper<EmqClientsDao>().eq(EmqClientsDao.COL_CLIENTID, clientId));
    }

    // TODO 本接口为前端轮询调用，并没有做数据缓存，所以会频繁的访问数据库，后期待优化
    /**
     * 返回给前端设备信息接口
     *
     * @return 设备信息list
     */
    public List<EmqClientsDao> getList() {
        return this.list();
    }

}
